// import { Controller, Get, Post } from '@nestjs/common';
// import { AppService } from './app.service';

// @Controller('app')
// export class AppController {
//   constructor(private readonly appService: AppService) {}

//   @Post('create')
//   create(): string {
//     return 'This action adds a new cat';
//   }

//   @Get('getStr')
//   getHello(): string {
//     return this.appService.getHello();
//   }
// }

// import { Controller, Get, Post, Req } from '@nestjs/common';
// import { Request } from 'express';

// @Controller('cats')
// export class AppController {
//   @Get()
//   findAll(@Req() request: Request): string {
//     console.log(111, request.url, request.method, request.query);
//     return 'This action returns all cats';
//   }
//   @Post('findAllPost')
//   findAllPost(@Req() request: Request): string {
//     console.log(111, request.url, request.method, request.query);
//     return 'This action returns all cats';
//   }
// }

import {
  Body,
  Controller,
  Get,
  HttpCode,
  Ip,
  Post,
  Req,
  Res,
  UseInterceptors,
  UploadedFiles,
  Inject,
  Query,
  UseFilters,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { CatService } from './cat.service';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { OtherService } from './other/other.service';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { ValidatePipe } from './validate.pipe';
import { TestFilter } from './test.filter';

class CreatePersonDto {
  name: string;
  age: number;
}

@Controller('person') // 指定路由前缀为 /cats
export class AppController {
  //构造器注入
  constructor(private readonly catService: CatService) {
    // 通常在这里注入服务类（例如 CatsService），以便处理业务逻辑
  }

  //通过属性声明注入
  @Inject(CatService)
  private catService2: CatService;

  @Inject(OtherService)
  private otherService: OtherService;

  // pipe 验证参数
  @Get('ccc')
  @UseFilters(TestFilter)
  ccc(@Query('num', ValidatePipe) num: number) {
    return num + 1;
  }

  @Get('getname')
  getname() {
    return 'Hello World!' + this.otherService.xxx();
  }

  // 定义一个 POST 请求处理程序，用于查找所有猫的信息
  @Post('findAll') // 这里定义了具体的路由路径，实际访问路径为 /cats/findAll
  async findAll(
    @Req() request: Request,
    @Res() response: Response,
  ): Promise<void> {
    console.log('Request URL:', request.url);
    console.log('Request Method:', request.method);
    console.log('Request Query Parameters:', request.body);

    // 示例响应数据
    const catsData = [
      { id: 1, name: 'Tom' },
      { id: 2, name: 'Jerry' },
    ];

    // 返回 JSON 数据
    response.status(200).json(catsData);
  }

  @Post('findAllPost')
  findAllPost(@Body() body: any, @Ip() ip: string) {
    console.log('Received body:', body);
    console.log('Received Ip:', ip);
    return this.catService.findAllPost(body);
  }

  @Post('file')
  @UseInterceptors(
    AnyFilesInterceptor({
      storage: diskStorage({
        destination: './uploads',
        filename: (req, file, cb) => {
          const uniqueSuffix =
            Date.now() + '-' + Math.round(Math.random() * 1e9);
          cb(
            null,
            file.fieldname + '-' + uniqueSuffix + extname(file.originalname),
          );
        },
      }),
    }),
  )
  body2(
    @Body() createPersonDto: CreatePersonDto,
    @UploadedFiles() files: Array<Express.Multer.File>,
  ) {
    console.log(files);
    return `received: ${JSON.stringify(createPersonDto)}`;
  }
}
